package com.cloudkinto.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.bo.system.NotificationAddBo;
import com.cloudkinto.bo.system.NotificationQueryBo;
import com.cloudkinto.bo.system.NotificationUpdateBo;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.dao.NotificationDao;
import com.cloudkinto.entity.NotificationDo;
import com.cloudkinto.service.system.NotificationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统通知公告表 service实现类
 * </p>
 *
 * @author long
 * @since 2024-04-30
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class NotificationServiceImpl extends ServiceImpl<NotificationDao, NotificationDo> implements NotificationService {

    @Override
    public void add(NotificationAddBo bo, Long userId) {
        NotificationDo entityDo = new NotificationDo();
        BeanUtils.copyProperties(bo, entityDo);
        handlerAddBefore(entityDo, userId);
        baseMapper.insert(entityDo);
    }

    //新增校验
    private void addCheck(Object req) {

    }

    /**
     * 做新增之前要执行的业务定制处理
     */
    private void handlerAddBefore(NotificationDo entityDo, Long userId) {
        entityDo.setCreateBy(userId);
        entityDo.setCreateTime(new Date());
        entityDo.setDeleteFlag(SysConstant.DELETE_FLAG_FALSE);
    }

    /**
     * 将新增的do转换成要返回的实体
     */
    private Object handlerAddAfter(NotificationDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public void update(NotificationUpdateBo bo, Long userId) {
        NotificationDo entityDo = new NotificationDo();
        BeanUtils.copyProperties(bo, entityDo);
        handlerUpdateBefore(entityDo, userId);
        baseMapper.updateById(entityDo);
    }

    /**
     * 做修改之前要执行的业务定制处理
     */
    private void handlerUpdateBefore(NotificationDo entityDo, Long userId) {
        entityDo.setUpdateBy(userId);
        entityDo.setUpdateTime(new Date());
    }

    /**
     * 做修改之后要执行的业务定制处理
     */

    @Override
    public int delete(Long id, Long userId) {
        NotificationDo entityDo = baseMapper.selectById(id);
//        handlerDeleteBefore(entityDo, userId);
        return baseMapper.deleteById(id);
    }

    //做删除之前要执行的业务定制处理
    /*private void handlerDeleteBefore(NotificationDo entityDo, Long userId) {
        entityDo.setDeleteBy(userId);
        entityDo.setDeleteTime(new Date());
    }*/

    @Override
    public NotificationDo detail(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询明细之后要执行的业务定制处理
     */
    private Object handlerDetailAfter(Object res) {
        return res;
    }


    @Override
    public PageResult<NotificationDo> getListPage(NotificationQueryBo bo) {
        IPage<NotificationDo> page = new Page<>(bo.getCurrent(), bo.getPageSize());
        page = this.page(page, queryBuild(bo));
        List<NotificationDo> resList = handlerListAfter(page.getRecords());
        return new PageResult<>(page.getRecords(), (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
    }

    /**
     * 查询分页列表之后对列表数据的处理
     */
    private List<NotificationDo> handlerListAfter(List<NotificationDo> list) {
        List<NotificationDo> result = new ArrayList<>();
        return result;
    }

    private LambdaQueryWrapper<NotificationDo> queryBuild(NotificationQueryBo bo) {
        LambdaQueryWrapper<NotificationDo> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), NotificationDo::getTitle, bo.getTitle());
        lqw.eq(Objects.nonNull(bo.getType()), NotificationDo::getType, bo.getType());
        if (Objects.nonNull(bo.getStatus())) {
            switch (bo.getStatus()) {
                case 0:
                    lqw.eq(NotificationDo::getStatus, 0);
                    break;
                case 1:
                    lqw.eq(NotificationDo::getStatus, 1);
                    break;
                case 2:
                    lqw.gt(NotificationDo::getEndTime, new Date());
                    break;
                default:
            }
        }
        lqw.ge(Objects.nonNull(bo.getStartDate()), NotificationDo::getCreateTime, bo.getStartDate());
        lqw.le(Objects.nonNull(bo.getEndDate()), NotificationDo::getCreateTime, bo.getEndDate());
        lqw.eq(NotificationDo::getDeleteFlag, SysConstant.DELETE_FLAG_FALSE);
        lqw.orderByDesc(NotificationDo::getCreateTime, NotificationDo::getId);
        return lqw;
    }


}
